home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14239 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Simulating a Memory Manager
  5. Date: 12 Apr 1996 11:27:52 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4km7b8INNt4o@keats.ugrad.cs.ubc.ca>
  8. References: <4kldef$png@news.service.uci.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4kldef$png@news.service.uci.edu>,
  12. Alex Wu <awu@hydra.acs.uci.edu> wrote:
  13.  >Hi, maybe you guys can help me, Im having problems moving around in memory.
  14.  >
  15.  >  Okay Im suppose to write a mock-memory manager, that keeps tracks of
  16.  >a list of pointers that points to memory blocks are in use, and a list
  17.  >of open 'holes' in the memory.  At the beginning of each block, there
  18.  >is a field for tag (nominate if it's full or open)  and size (# of bytes
  19.  >in use).
  20.  
  21.  >char *blocks[100];    /* keeps tracks of pointers that points to the
  22.  >             used blocks */
  23.  
  24. This is lame, because it restricts you to allocating only a total of 100 blocks
  25. of any. It would be better to keep the blocks in a linked list _within_
  26. the actual allocation zone.
  27.  
  28.  >struct template
  29.  >{
  30.  >  short tag;
  31.  >  short size;
  32.  >};
  33.  >
  34.  >char MM[32768];        /* size of the memory block we are keeping */
  35.  >char *MEM_PTR = MM;    /* points to the beginning of the block */
  36.  >
  37.  >
  38.  > Okay, we first initialize the memory, so it's just one big hole.
  39.  >
  40.  >struct template *HEAD
  41.  >
  42.  >init_mem()
  43.  >{
  44.  >  HEAD->tag = TRUE;     /* it's a hole */
  45.  >  HEAD->size = 32768;
  46.  >}
  47.  
  48. You are missing a semicolon after HEAD. Secondly, HEAD doesn't point anywhere,
  49. so you can't assign to HEAD->tag or HEAD->size.
  50.  
  51.  >  Now it has to handle requests.  n is the number of bytes that I have to
  52.  >set aside.  Once I set it aside, I have to return the begining of the block.
  53.  >And of course adjust any open holes.
  54.  
  55. Have a look at the example malloc() implementation in the K&R2, the chapter on
  56. the UNIX system interface, keeping in mind that ANSI C prohibits the external
  57. redefinition of standard library functions like malloc() in a hosted
  58. environment.
  59. -- 
  60.  
  61.